package ullio.utils
{	
	
	public class StringUtils
	{
		
		public static function replaceAll(input:String, replace:String, replaceWith:String):String
		{
			//change to StringBuilder
			var sb:String = new String();
			var found:Boolean = false;

			var sLen:Number = input.length;
			var rLen:Number = replace.length;

			for (var i:Number = 0; i < sLen; i++)
			{
				if(input.charAt(i) == replace.charAt(0))
				{   
					found = true;
					for(var j:Number = 0; j < rLen; j++)
					{
						if(!(input.charAt(i + j) == replace.charAt(j)))
						{
							found = false;
							break;
						}
					}

					if(found)
					{
						sb += replaceWith;
						i = i + (rLen - 1);
						continue;
					}
				}
				sb += input.charAt(i);
			}
			return sb;
		}
		
		
		public static function stringsAreEqual(s1:String, s2:String, caseSensitive:Boolean):Boolean
		{
			if(caseSensitive)
			{
				return (s1 == s2);
			}
			else
			{
				return (s1.toUpperCase() == s2.toUpperCase());
			}
		}
		
	
		public static function trim(input:String):String
		{
			return StringUtils.ltrim(StringUtils.rtrim(input));
		}


		public static function ltrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = 0; i < size; i++)
			{
				if(input.charCodeAt(i) > 32)
				{
					return input.substring(i);
				}
			}
			return "";
		}

		public static function rtrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = size; i > 0; i--)
			{
				if(input.charCodeAt(i - 1) > 32)
				{
					return input.substring(0, i);
				}
			}

			return "";
		}

		public static function contains(input:String, search:String):Boolean{
			return input.indexOf(search) > -1;
		}
		
		public static function beginsWith(input:String, prefix:String):Boolean
		{			
			return (prefix == input.substring(0, prefix.length));
		}	


		public static function endsWith(input:String, suffix:String):Boolean
		{
			return (suffix == input.substring(input.length - suffix.length));
		}	


		public static function firstChar(input:String):String 
    	{
  			return input.charAt(0);
    	}
    	
		public static function lastChar(input:String):String 
 	    {
    		return input.charAt(input.length - 1);
	    }
	    
		public static function isEmpty(input:String):Boolean 
	    {
		    return input.length == 0;
    	}    	
    			
		public static function reverse(input:String):String 
	    {  
		    var ar:Array = input.split("");
    		ar.reverse();
	    	return ar.join("");
	    }
		
		public static function ucFirst(input:String):String 
	    {
		    return input.charAt(0).toUpperCase() + input.substring(1);
	    }
		
		public static function ucWords(input:String):String 
	    {
		    var ar:Array = input.split(" ");
		    var l:uint = ar.length ;
		    while(--l > -1) 
		    {
		        ar[l] = ucFirst(ar[l]);
		    }
		    return ar.join(" ");
	    }
	
		public static function lastIndexOfAny(input:String, ar:Array):int 
    	{
    		var index:int = -1;
    		var l:uint = ar.length;
    		for (var i:Number = 0 ; i<l ; i++) 
    		{
    			index = input.lastIndexOf(ar[i]);
    			if (index > -1) 
    			{
    				return index;
    			}
    		}
    		return index;
	    }
	
	 	public static function copy(input:String):String 
        {
		    return input.valueOf();
	    }
	    
		public static function clone(input:String):String 
        {
		    return input;	
    	}
	}
}